<?php

class ModuleResultOfOSAndBrowser extends CmsCustomModule {

    //set the public head
    private function setHeadString($condition=null,$sort_by=null,$rand_code) {
        $date_list = null;
        $current_time = date('Y-m-d H:i:s');
        $region_country_list = ' None ';
       
        if (!empty($condition) && is_array($condition)) {
            //date time.
            if (isset($condition['play_time']['start_date']) && isset($condition['play_time']['end_date'])) {
                $date_list = '<strong>By Date: </strong>&nbsp; <b>From</b> '.$condition['play_time']['start_date'].' <b>To</b> '.$condition['play_time']['end_date'];
            }
            else {
                $date_list = '<b>By Month: </b>&nbsp;'.$condition['play_time']['start_month'].' <b>To</b> '.$condition['play_time']['end_month'];
            }
            
            //region & country
            if (!empty($condition['country_code'])) {
                $country_arr = $condition['country_code'];
                $region_country_list = AC::getInstance()->getCountryListToHeader($country_arr);
            }
        }

        $condition_list = '<br/>'.
                          '<div>'.$date_list.'</div>'.
			              '<table class="nob">'.
                          '<tr>'.
                          ' <td valign="top" width="110">'.
                          '     <b class="tit">Region/Country: </b>&nbsp;</td>'.
                          ' <td valign="top">'.$region_country_list.'</td>'.
                          '</tr>'.
                          '</table>';

       //control tag.                         
        $export_tag =  'Export to '.AC::exportHtmlControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_VIDEO_OS_BROWSER)
                                   .AC::exportPdfControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_VIDEO_OS_BROWSER)
                                   .AC::exportCvsControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_VIDEO_OS_BROWSER);

        $head_string = '<div class="hd">'.
                       '    <div class="utility">'.
                       '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$export_tag.
                       '    </div>'.
                       '    <hr>'.
                       '    <h1>'.$this->title.'</h1>'.
                       '    <div class="ui-helper-clearfix">'.
                       '        <div class="time">'.$current_time.'</div>'.
                       '        <ul>'.$condition_list.'</ul>'.
                       '    </div>'.
                       '</div>';

        $head_string_exprot = '<div class="hd">'.
                                '    <div class="utility">'.
                                '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                                '    </div>'.
                                '    <hr>'.
                                '    <h1>'.$this->title.'</h1>'.
                                '    <div class="ui-helper-clearfix">'.
                                '        <div class="time">'.$current_time.'</div>'.
                                '        <ul>'.$condition_list.'</ul>'.
                                '    </div>'.
                                '</div>';

        return array('view'=>$head_string,'export'=>$head_string_exprot);
    }

    //out put the body string.
    private function getOutPutBody($head_string,$body_string) {
        $output ='<div id="result" class="os_browser_result" >'.$head_string.
                 '  <div class="bd">'.
                    $body_string.
                 '  </div>'.
                 '</div>';
        return $output;
    }

    //get the page search filter condition.
    private function getPageCondition($params) {
        $date = array();
        if (!empty($params['startdate']) && !empty($params['enddate'])) {
            $date['start_date'] = $params['startdate'];
            $date['end_date'] = $params['enddate'];
            if ($params['enddate']<$params['startdate']) $date['end_date'] = date('Y-m-d');
        }

        $month = array();
        if (!empty($params['startmonth']) && !empty($params['endmonth'])) {
            $month['start_month'] = $params['startyear'].'-'.$params['startmonth'];
            $month['end_month'] = $params['endyear'].'-'.$params['endmonth'];
        }
        $play_time = ($params['dateby']=='month') ? $month : $date;


        $country = array();
        if (!empty($params['country_code'])) {
            $country = (!empty($params['country_code'])) ? json_decode($params['country_code'],true) : array();
        }

        //os & browser.
        $os_browser = array();
        if (!empty($params['os_browser'])) {
            $os_browser_str = urldecode($params['os_browser']);
            $os_browser_arr_tmp = explode("\n",$os_browser_str);
            foreach($os_browser_arr_tmp as $list_tmp) {
                $tmp_item = explode("-",$list_tmp);
                $tmp_os = $tmp_item[0];
                $tmp_browser = $tmp_item[1];
                if (!isset($os_browser[$tmp_os])) $os_browser[$tmp_os] = array();
                if (!isset($os_browser[$tmp_os][$tmp_browser])) $os_browser[$tmp_os][$tmp_browser] = $tmp_browser;
            }
        }

        return array('condition'=>array('play_time'=>$play_time,'country_code'=>$country,'combination'=>$os_browser));
    }

    //get the table name.
    private function getDateCondition($play_time,$table_alias) {
        $table_name = array();
        $date_condition = '';
        if (!empty($play_time['start_date']) && !empty($play_time['end_date'])) {
            $date_condition .= ' AND (SUBSTRING('.$table_alias.'.play_time,1,10)>="'.$play_time['start_date'].'" AND SUBSTRING('.$table_alias.'.play_time,1,10)<="'.$play_time['end_date'].'") ';
            $start_ = date('Y',strtotime($play_time['start_date']));
            $end_ = date('Y',strtotime($play_time['end_date']));
            for($i=$start_;$i<=$end_;$i++) {
                $table_name['year'][] = $i;
                $table_name['month'] = 0;
            }
        }
        elseif(!empty($play_time['start_month']) && !empty($play_time['end_month'])) {
            if ($play_time['start_month']==$play_time['end_month']) {
                $table_name['year'] = date('Y',strtotime($play_time['start_month']));
                $table_name['month'] = date('m',strtotime($play_time['start_month']));
                $date_condition .= ' AND (SUBSTRING('.$table_alias.'.play_time,1,7)="'.$table_name['year'].'-'.$table_name['month'].'") ';
            }
            else {
                $date_condition .= ' AND (SUBSTRING('.$table_alias.'.play_time,1,10)>="'.$play_time['start_month'].'" AND SUBSTRING('.$table_alias.'.play_time,1,10)<="'.$play_time['end_month'].'") ';
                $start_ = date('Y',strtotime($play_time['start_month']));
                $end_ = date('Y',strtotime($play_time['end_month']));
                for($i=$start_;$i<=$end_;$i++) {
                    $table_name['year'][] = $i;
                    $table_name['month'] = 0;
                }
            }
        }

        return array('table_name'=>$table_name,'condition'=>$date_condition);
    }
    
    //Optimization
    private function getDataConditionOptimization($play_time) {
        $date_condition = null;
        if (!empty($play_time['start_date']) && !empty($play_time['end_date'])) {
            $date_condition = ' AND (day_time >="'.$play_time['start_date'].'" AND day_time<="'.$play_time['end_date'].'") '; 
        }
        elseif(!empty($play_time['start_month']) && !empty($play_time['end_month'])) {
            $date_condition = ' AND (SUBSTRING(day_time,1,7)>="'.$play_time['start_month'].'" AND SUBSTRING(day_time,1,7)<="'.$play_time['end_month'].'") ';
        }
        return array('condition'=>$date_condition);
    }

    //set the no datas.
    private function setMainBodyListHaveNone($countries=null) {
        if (empty($countries)) {
            $data = array('summary'=>array('os'=>array(),'browser'=>array()));
            return $this->setPicChartLineOfBody($data,array());
        }
        else {
            $data = array();
            foreach($countries as $region=>$region_val) {
                foreach($region_val as $code=>$name) {
                    $data[$region][$code] = array('os'=>array(),'browser'=>array());
                }
            }
            return $this->setPicChartLineOfBody($data,$countries);
        }
    }

    //set a blank line.
    private function setBlankLineWithMessage($flag,$message=null) {
        $tmp_title = ($flag) ? AC::VIDEO_VIEWING_REPORT_REGION_COUNTRY_GROUP : AC::VIDEO_VIEWING_REPORT_TOTAL_SUMMARY;
        $message = (!empty($message)) ? $message : ErrorMessage::HAVE_NO_DATA;
        $body ='<dl>'.
               '    <dt>'.$tmp_title.'</dt>'.
               '    <table><tr height="60"><td align="center">'.$message.'</td></tr></table>'.
               '</dl>';
        return $body;
    }
    
    private function filterDataWithCondition($data,$type) {
        $tmp_data = array();
        //if type is true or 1,the country is null,will count by total.
        if ($type) {
            foreach($data as $key=>$list) {
                if (!isset($tmp_data[$key])) $tmp_data[$key] = array();
                if (!isset($tmp_data[$key]['os'])) $tmp_data[$key]['os'] = array();
                foreach($list['os'] as $os_k=>$os_v) {
                    $actual_os_name = AC::filterAndMergeOs($os_k);
                    if (!isset($tmp_data[$key]['os'][$actual_os_name])) $tmp_data[$key]['os'][$actual_os_name] = array();
                    foreach($os_v as $b_k=>$b_v) {
                        $actual_browser_name = AC::filterAndMergeBrowser($b_k);
                        if (!isset($tmp_data[$key]['os'][$actual_os_name][$actual_browser_name])) $tmp_data[$key]['os'][$actual_os_name][$actual_browser_name] = 0;
                        $tmp_data[$key]['os'][$actual_os_name][$actual_browser_name] += $b_v;
                    }
                }
            }
        }
        else {
            //count by country.
            foreach($data as $region=>$region_val) {
                if (!isset($tmp_data[$region])) $tmp_data[$region] = array();
                foreach($region_val as $code=>$code_val) {
                    if (!isset($tmp_data[$region][$code])) $tmp_data[$region][$code] = array();
                    foreach($code_val as $os_key=>$list) {
                        if (!isset($tmp_data[$region][$code][$os_key])) $tmp_data[$region][$code][$os_key] = array();
                        $actual_os_name = AC::filterAndMergeOs($os_key);
                        if (!isset($tmp_data[$region][$code][$actual_os_name])) $tmp_data[$region][$code][$actual_os_name] = array();
                        foreach($list as $b_k=>$b_v) {
                            $actual_browser_name = AC::filterAndMergeBrowser($b_k);
                            if (!isset($tmp_data[$region][$code][$actual_os_name][$actual_browser_name])) $tmp_data[$region][$code][$actual_os_name][$actual_browser_name] = 0;
                            $tmp_data[$region][$code][$actual_os_name][$actual_browser_name] += $b_v;
                        }
                    }
                }
            }
        }
        if (isset($data)) unset($data);
        return $tmp_data;
    }

    //get the os and browser data.
    private function getOSAndBrowserPieChartList($play_time,$countries_code,$pegging_countries_code,$combination=null) {
        $data = array();
        $tmp_date_arr = $this->getDataConditionOptimization($play_time);
        $date_condition = $tmp_date_arr['condition'];
        $condition = $date_condition;
        //get data from db.
        $obj = new ModelReportPages();
        $data = $obj->OSAndBrowserOfCountryOptimization($countries_code,$condition,$pegging_countries_code,$combination);
        if (!empty($combination)) {
            //filter data with xml group conditions.
            $type = (empty($countries_code)) ? 1 : 0;
            $data = $this->filterDataWithCondition($data,$type);
        }
        return $data;
    }
    
    //compare the data.
    private function compareData($data,$default,$pattern) {
        $result = array();
        if (empty($data)) {
            foreach($default as $default_key) {
                $data[] = 0;
            }
        }
        else {
            $data_keys = array_keys($data);
        }

        foreach($data_keys as $k=>$bo_list) {
            $pattern_flag = false;
            foreach($pattern as $bo_k=>$bo_v) {
                if (!isset($result[$bo_k])) $result[$bo_k] = 0;
                foreach($bo_v as $pattern_val) {
                    if (eregi($pattern_val,$bo_list)==true) {
                        $pattern_flag = true;
                        $result[$bo_k]+=(int)$data[$bo_list];
                    }
                }
            }
            if (!$pattern_flag) {
                if (!isset($result['Other'])) $result['Other'] = 0;
                $result['Other']+=(int)$data[$bo_list];
            }
        }

        if (isset($data)) unset($data);
        
        $data = array();
        foreach($result as $val) {
            $data[] = (int)$val;
        }
        return $data;
    }
    
    //get the pie chart
    private function getPicChartLine($country,$stat,$stat_country,$data=null) {
        $current_country_total = count($country);
        $real_country_total = count($stat_country[$stat]);
        $video_default_none = AC::$video_default_none;
        //Browser Team
        $video_default_browser = AC::getBrowserTeamForView();
        $video_default_browser_keys = array_keys($video_default_browser);

        //OS Team
        $video_default_os = AC::getInstance()->getOSTeamForView();
        $video_default_os_keys = array_keys($video_default_os);
        
        //
        $video_default_browser_text = '["'.implode('","',$video_default_browser_keys).'"]';
        $video_default_os_text = '["'.implode('","',$video_default_os_keys).'"]';
        
        //None.
        $video_default_none_text = json_encode(array_keys($video_default_none));
        $output = null;

        //if country is not null
        if (!empty($stat) && !empty($stat_country)) {
            //if the countries total more than 1.
            if ($current_country_total>1) {
                $show_country_text = ($current_country_total==$real_country_total) ? 'All Countries' : 'Some Countries';
                $region_total = array();
                //add all countries.

                $c_flag = false;
                foreach($country as $code=>$list) {
                    if (isset($data[$stat][$code]) && is_array($data[$stat][$code])) {
                        foreach($data[$stat][$code] as $bo_k=>$bo_v) {
                            if (!isset($region_total[$bo_k])) $region_total[$bo_k] = array();
                            foreach($bo_v as $bo_k2=>$bo_v2) {
                                $c_flag = true;
                                if (!isset($region_total[$bo_k][$bo_k2])) $region_total[$bo_k][$bo_k2] = 0;
                                $region_total[$bo_k][$bo_k2] += $bo_v2;
                            }
                        }
                    }
                }

                //all countries has none data.
                if (!$c_flag) {
                    $region_total = array('os'=>array(),'browser'=>array());
                }

                $output .= '<dd style="padding:5px;"><center><b>'.$stat.' ('.$show_country_text.')'.'</b></center></dd>'.
                           '<dd><center>';
                foreach($region_total as $bo_k=>$bo_v) {
                    $video_default_val = null;
                    $video_default_txt = null;
                    $out_list = null;
                    $bo_k_name = (strtoupper($bo_k)=='OS') ? 'OS' : ucwords($bo_k);
                    if (strtolower($bo_k)=='broswer' && !empty($bo_v)) {
                        $video_default_val = $video_default_browser_keys;
                        $video_default_txt = $video_default_browser_text;
                    }
                    elseif(strtolower($bo_k)=='os' && !empty($bo_v)) {
                        $video_default_val = $video_default_os_keys;
                        $video_default_txt = $video_default_os_text;
                    }
                    else {
                        $video_default_val = array();
                        $video_default_txt = $video_default_none_text;
                    }

                    if (!empty($bo_v)) {
                        $out_list = (strtolower($bo_k)=='broswer') ? $this->compareData($bo_v,$video_default_browser_keys,$video_default_browser) : ((strtolower($bo_k)=='os') ? $this->compareData($bo_v,$video_default_os_keys,$video_default_os) : array($video_default_none['NONE']));
                    }
                    else {
                        $out_list = array($video_default_none['NONE']);
                    }
                    $class_ = (!empty($bo_v)) ? 'countrychart2' : 'countrychart none';
                    $capture = $video_default_txt;
                    $output .= '<input type="hidden" class="'.$class_.'" name="'.$bo_k_name.'" value="'.json_encode($out_list).'" capture=\''.$capture.'\' />';
                }
                $output .= '</center></dd>';
            }

            //list all iteam.
            foreach($country as $c_code=>$c_val) {
                $output .= '<dd style="padding:5px;"><center><b>'.$stat_country[$stat][$c_code][0].' ('.$c_code.')'.'</b></center></dd>'.
                           '<dd><center>';
                    if (!isset($data[$stat][$c_code])) {
                        $data[$stat][$c_code] = array('os'=>array(),'browser'=>array());
                    }
                    foreach($data[$stat][$c_code] as $bo_k=>$bo_v) {
                        $video_default_val = null;
                        $video_default_txt = null;
                        $bo_k_name = (strtoupper($bo_k)=='OS') ? 'OS' : ucwords($bo_k);
                        if (strtolower($bo_k)=='broswer' && !empty($bo_v)) {
                            $video_default_val = $video_default_browser_keys;
                            $video_default_txt = $video_default_browser_text;
                        }
                        elseif(strtolower($bo_k)=='os' && !empty($bo_v)) {
                            $video_default_val = $video_default_os_keys;
                            $video_default_txt = $video_default_os_text;
                        }
                        else {
                            $video_default_val = array();
                            $video_default_txt = $video_default_none_text;
                        }

                        if (!empty($bo_v)) {
                            $out_list = (strtolower($bo_k)=='broswer') ? $this->compareData($bo_v,$video_default_browser_keys,$video_default_browser) : ((strtolower($bo_k)=='os') ? $this->compareData($bo_v,$video_default_os_keys,$video_default_os) : array($video_default_none['NONE']));
                        }
                        else {
                            $out_list = array($video_default_none['NONE']);
                        }
                        //$out_list = (!empty($bo_v)) ? $this->compareData($bo_v,$video_default_val) : array($video_default_none['NONE']);
                        $class_ = (!empty($bo_v)) ? 'countrychart2' : 'countrychart2 none';
                        $capture = $video_default_txt;
                        $output .= '<input type="hidden" class="'.$class_.'" name="'.$bo_k_name.'" value="'.json_encode($out_list).'" capture=\''.$capture.'\' />';
                    }
                //}
                $output.='</center></dd>';
            }
        }
        else {
            //total summary
            foreach($country as $bo_k=>$bo_v) {
                $video_default_val = null;
                $video_default_txt = null;
                $bo_k_name = (strtoupper($bo_k)=='OS') ? 'OS' : ucwords($bo_k);
                if (strtolower($bo_k)=='broswer') {
                    $video_default_val = $video_default_browser_keys;
                    $video_default_txt = $video_default_browser_text;
                }
                elseif(strtolower($bo_k)=='os') {
                    $video_default_val = $video_default_os_keys;
                    $video_default_txt = $video_default_os_text;
                }
                else {
                    $video_default_val = array();
                    $video_default_txt = $video_default_none_text;
                }

                if (!empty($bo_v)) {
                    $out_list = (strtolower($bo_k)=='broswer') ? $this->compareData($bo_v,$video_default_browser_keys,$video_default_browser) : ((strtolower($bo_k)=='os') ? $this->compareData($bo_v,$video_default_os_keys,$video_default_os) : array($video_default_none['NONE']));
                }
                else {
                    $out_list = array($video_default_none['NONE']);
                }
                //$out_list = (!empty($bo_v)) ? $this->compareData($bo_v,$video_default_val) : array($video_default_none['NONE']);
                $class_ = (!empty($bo_v)) ? 'countrychart' : 'countrychart none';
                $capture = $video_default_txt;
                $output .= '<center><input type="hidden" class="'.$class_.'" name="'.$bo_k_name.'" value="'.json_encode($out_list).'" capture=\''.$capture.'\' /></center>';
            }
        }

        //return pie chart.
        return $output;
    }

    //write the line with pic chart
    private function setPicChartLineOfBody($data,$countries_code) {
        $body_list = null;
        if (!empty($countries_code)) {
            foreach($countries_code as $key=>$value) {
                $stat_country = getStatsClass::getInstance()->getAllCountryByStat($key);
                $body_list .= '<dl><dt>'.$key.'</dt>'.$this->getPicChartLine($value,$key,$stat_country,$data).'</dl>';
            }
        }
        else {
            //total summary
            $body_list.='<dl>'.
                        '   <dt>'.
                        '       <center>Total Summary</center>'.
                        '   </dt>'.
                        '   <dd>'.$this->getPicChartLine($data['summary'],null,null).'</dd>'.
                        '</dl>';
        }
        return $body_list;
    }

    //get and return the table string.
    private function getTableLine($country,$stat,$stat_country,$data=null,$combination) {

        $body = null;
        $table_line_title = '<tr class="weight">'.
                            '    <td width="194" bgcolor="#cccccc">OS</td>'.
                            '    <td width="195" bgcolor="#cccccc">Browser</td>'.
                            '    <td width="117" bgcolor="#cccccc" class="num">Hits</td>'.
                            '    <td width="95" bgcolor="#cccccc" class="num">Percent</td>'.
                            '    <td width="49" bgcolor="#cccccc" class="num">&nbsp;</td>'.
                            '</tr>';
        if (!empty($stat) && !empty($stat_country)) {
            //select the countries.
            foreach($country as $c_code=>$c_val) {
                //calculate total num of each OS.
                $new_data_os = array();
                foreach($combination as $c_k_tmp=>$c_v_tmp) {
                    $c_k_tmp_ = trim($c_k_tmp);
                    if (!isset($new_data_os[$c_k_tmp_])) $new_data_os[$c_k_tmp_] = 0;
                    $tmp_nums_ = 0;
                    foreach($c_v_tmp as $b_k_tmp=>$b_v_tmp) {
                        $b_k_tmp_ = trim($b_k_tmp);
                        $tmp_nums_ += (isset($data[$stat][$c_code][$c_k_tmp_][$b_k_tmp_])) ? $data[$stat][$c_code][$c_k_tmp_][$b_k_tmp_] : 0;    
                    }
                    $new_data_os[$c_k_tmp_] = $tmp_nums_;
                }

                //output body.
                $body .='<dd style="padding:5px;"><center><b>'.$stat_country[$stat][$c_code][0].' ('.$c_code.')'.'</b></center></dd>'.
                        '<table border="0" cellpadding="0" cellspacing="0">'.
                        '  <tbody>'.$table_line_title;                
                foreach($combination as $c_k=>$c_v) {
                    //$total_os_ave = 0;
                    $rows_span = count($c_v)+1;
                    $body .='<tr><td colspan="5" class="weight">'.$c_k.'</td></tr>';
                    $c_k_ = trim($c_k);
                    $n = 1;
                    $total_each_os = (isset($new_data_os["$c_k_"])) ? intval($new_data_os["$c_k_"]) : 0;
                    $actual_number_arr = (isset($data[$stat][$c_code]["$c_k_"])) ? $data[$stat][$c_code]["$c_k_"] : array();
                    foreach($c_v as $cc_k=>$cc_v) {
                        $cc_k_ = trim($cc_k);
                        $rowspan_str = ($n==1) ? '<td rowspan="'.$rows_span.'">&nbsp;</td>' : null;
                        $actual_number = (isset($actual_number_arr[$cc_k_])) ? $actual_number_arr[$cc_k_] : 0;
                        $actual_ave_num = ($total_each_os==0) ? 0 : round(($actual_number/$total_each_os)*100,2);

                        $body .='<tr>'.$rowspan_str.
                                '   <td>'.$cc_v.'</td>'.
                                '   <td class="num">'.number_format($actual_number).'</td>'.
                                '   <td class="num">'.$actual_ave_num.'%</td>'.
                                '   <td class="num">&nbsp;</td>'.
                                '</tr>';
                        $n++;
                        //$total_os_ave += $actual_ave_num;
                    }
                    $body.= '<tr>'.
                            '   <td class="total"><strong>Total</strong></td>'.
                            '   <td class="num"><strong>'.number_format($total_each_os).'</strong></td>'.
                            '   <td class="num"><strong>&nbsp;</strong></td>'.
                            '   <td class="num">&nbsp;</td>'.
                            '</tr>';
                }
                $body .='</tbody></table>';
            }

        }
        else {
            //calculate total num of each OS.
            $new_data_os = array();
            foreach($combination as $c_k_tmp=>$c_v_tmp) {
                $c_k_tmp_ = trim($c_k_tmp);
                if (!isset($new_data_os[$c_k_tmp_])) $new_data_os[$c_k_tmp_] = 0;
                $tmp_nums_ = 0;
                foreach($c_v_tmp as $b_k_tmp=>$b_v_tmp) {
                    $b_k_tmp_ = trim($b_k_tmp);
                    $tmp_nums_ += (isset($country['os'][$c_k_tmp_][$b_k_tmp_])) ? $country['os'][$c_k_tmp_][$b_k_tmp_] : 0;    
                }
                $new_data_os[$c_k_tmp_] = $tmp_nums_;
            }
            
            //setting body.
            $body .= '<dd style="padding:5px;"><center><b>&nbsp;</b></center></dd>'.
                    '<table border="0" cellpadding="0" cellspacing="0">'.
                    '   <tbody>'.$table_line_title;
            foreach($combination as $c_k=>$c_v) {
                //$total_os_ave = 0;
                $rows_span = count($c_v)+1;
                $body .='<tr><td colspan="5" class="weight">'.$c_k.'</td></tr>';
                $c_k_ = trim($c_k);
                $n = 1;
                $total_each_os = (isset($new_data_os["$c_k_"])) ? intval($new_data_os["$c_k_"]) : 0;
                $actual_number_arr = (isset($country['os']["$c_k_"])) ? $country['os']["$c_k_"] : array();
                foreach($c_v as $cc_k=>$cc_v) {
                    $cc_k_ = trim($cc_k);
                    $rowspan_str = ($n==1) ? '<td rowspan="'.$rows_span.'">&nbsp;</td>' : null;
                    $actual_number = (isset($actual_number_arr[$cc_k_])) ? $actual_number_arr[$cc_k_] : 0;
                    $actual_ave_num = ($total_each_os==0) ? 0 : round(($actual_number/$total_each_os)*100,2);
                    $body .='<tr>'.$rowspan_str.
                            '   <td>'.$cc_v.'</td>'.
                            '   <td class="num">'.number_format($actual_number).'</td>'.
                            '   <td class="num">'.$actual_ave_num.'%</td>'.
                            '   <td class="num">&nbsp;</td>'.
                            '</tr>';
                    $n++;
                    //$total_os_ave += $actual_ave_num;
                }
                $body .='<tr>'.
                        '   <td class="total"><strong>Total</strong></td>'.
                        '   <td class="num"><strong>'.number_format($total_each_os).'</strong></td>'.
                        '   <td class="num"><strong>&nbsp;</strong></td>'.
                        '   <td class="num">&nbsp;</td>'.
                        '</tr>';
            }
            $body .= '</tbody></table>';
        }
        return $body;
    }

    //write the line with the table rows.
    private function setTableLineOfBody($data,$countries_code,$combination) {
        $body_list = null;
        if (!empty($countries_code)) {
            //region&country group.
            foreach($countries_code as $key=>$value) {
                $stat_country = getStatsClass::getInstance()->getAllCountryByStat($key);
                $body_list .= '<dl><dt>'.$key.'</dt>'.$this->getTableLine($value,$key,$stat_country,$data,$combination).'</dl>';
            }
        }
        else {
            //total summary.
            $body_list .=   '<dl>'.
                            '   <dt>'.
                            '       <center>'.AC::VIDEO_VIEWING_REPORT_TOTAL_SUMMARY.'</center>'.
                            '   </dt>'.
                            '   <dd>'.$this->getTableLine($data['summary'],null,null,null,$combination).'</dd>'.
                            '</dl>';
        }
        return $body_list;
    }


    //set os and browser body string.
    private function setOSAndBrowserPieChartBodyString($condition) {
        //if not select the countries then show total.
        if (empty($condition['country_code'])) {
            return $this->setTotalSummaryPieChartBodyString($condition);
        }

        $pegging_countries_code = array();
        $countries_code = array();
        $country_code = array();
        $body_list = null;
        $data = array();
        //country code.
        $country_code = AC::getInstance()->getRegionCountryCondtion($condition['country_code']);
        $pegging_countries_code = AC::getInstance()->getPegginCountryCode($country_code);
        if (!empty($country_code)) {
            $countries_code = AC::getInstance()->getCountryCodesListByRegion($country_code);
        }
        $data = $this->getOSAndBrowserPieChartList($condition['play_time'],$countries_code,$pegging_countries_code,$condition['combination']);
        if (empty($condition['combination'])) {
            $body_list .= (!empty($data)) ? $this->setPicChartLineOfBody($data,$countries_code) : $this->setBlankLineWithMessage(1);
        }
        else {
            $body_list .= (!empty($data)) ? $this->setTableLineOfBody($data,$countries_code,$condition['combination']) : $this->setBlankLineWithMessage(1);
        }
        return $body_list;
    }

    //set the total summary
    private function setTotalSummaryPieChartBodyString($condition) {
        $body_list = null;
        $data = array();
        $data = $this->getOSAndBrowserPieChartList($condition['play_time'],array(),array(),$condition['combination']);
        if (empty($condition['combination'])) {
            $body_list .= (!empty($data['summary'])) ? $this->setPicChartLineOfBody($data,array()) : $this->setBlankLineWithMessage(0);
        }
        else {
            $body_list .= (!empty($data['summary'])) ? $this->setTableLineOfBody($data,null,$condition['combination']) : $this->setBlankLineWithMessage(0);
        }
        return $body_list;
    }

    //entrance
    protected function __renderHead() {
        $body_string = null;
        $rand_code = rand(10000,99999);

        //parameters re-assignment.
        $conditions = $_GET;
        $conditions['country_code'] = $conditions['country'];
        $conditions = $this->getPageCondition($conditions);

        //get the header string.
        $head_string = $this->setHeadString($conditions['condition'],null,$rand_code);
        
        //body string.
        $body_string .= $this->setOSAndBrowserPieChartBodyString($conditions['condition']);

        //write a tmp file Export Tool.
        $export_html_string = $this->getOutPutBody($head_string['export'],$body_string);
        $write_result = AC::getInstance()->writeTempBodyToFile($rand_code,$export_html_string);
        
        //output.
        $this->contents =$this->getOutPutBody($head_string['view'],$body_string);
        
    }

}

?>